<template>
  <page-model
  :breadHeard=breadHeard
  @backLast="backLast"
  >
  <desc-title
  class="mt-20"
  :descTitle=descTitle
  >
  </desc-title>
  <div class="content">
    <el-form :model="sysUserDTO " :rules="rules" ref="sysUserDTO" label-width="100px">
    <el-form-item label="姓名：" prop="name">
      <el-input v-model="sysUserDTO.name" maxlength="10"></el-input>
    </el-form-item>
    <el-form-item label="手机号码：" prop="mobile">
      <el-input v-model="sysUserDTO.mobile" placeholder="请输入手机号"></el-input>
    </el-form-item>
    <el-form-item label="部门："  prop="webDeptId" >
      <el-select v-model="sysUserDTO.webDeptId" placeholder="请选择部门">
        <el-option
          v-for="item in departList"
          :key="item.webDeptId"
          :label="item.webDeptName"
          :value="item.webDeptId"
          >
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="职位："  placeholder="请输入职位">
      <el-input v-model="sysUserDTO.position" maxlength="10"></el-input>
    </el-form-item>
    <el-form-item label="角色："  prop="roleId" >
      <el-select v-model="sysUserDTO.roleId" placeholder="请选择角色">
        <el-option
          v-for="item in allRoleList"
          :key="item.roleId"
          :label="item.roleName"
          :value="item.roleId"
          >
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="账号状态：" prop="status" placeholder="请选择状态">
      <el-select v-model="sysUserDTO.status" placeholder="请选择状态">
        <el-option label="启用" :value=1></el-option>
        <el-option label="禁用" :value=0></el-option>
      </el-select>
    </el-form-item>
    </el-form>
    <div class="addBtn">
      <span class="confirm flex-center" @click="confirmAdd">确定</span>
    </div>
  </div>
</page-model>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data () {
    return {
      breadHeard: {
        title: '添加员工',
        firstBread: '项目员工管理',
        secondBread: '添加员工'
      },
      descTitle: '员工信息',
      sysUserDTO: {
        name: '',
        mobile: '',
        webDeptId: '',
        position: '',
        roleId: '',
        status: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入视频名称', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请添加视频封面', trigger: 'change' }
        ],
        webDeptId: [
          { required: true, message: '请添加视频封面', trigger: 'change' }
        ],
        position: [
          { required: true, message: '请输入职位', trigger: 'change' }
        ],
        roleId: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    ...mapState('projectRoleManage', [
      'departList',
      'allRoleList'
    ]),
    userId () {
      return sessionStorage.getItem('userId')
    }
  },
  methods: {
    backLast () {
      this.$router.push('/innerRoleManageMain')
    },
    confirmAdd () {
      const params = { ...this.sysUserDTO }
      this.$refs.sysUserDTO.validate((valid) => {
        if (valid) {
          if (this.userId) { // 编辑
            this.$store.dispatch('projectRoleManage/editRoleData', { sysUserDTO: params, userId: this.userId }).then(res => {
              if (res.data.code === 200) {
                this.$message({
                  message: '编辑成功',
                  type: 'success',
                  duration: 500
                })
                this.$router.push('/innerRoleManageMain')
              }
            })
            return
          }
          // 添加
          this.$store.dispatch('projectRoleManage/addRoleItem', params).then(res => {
            if (res.data.code === 200) {
              this.$message({
                message: '添加成功',
                type: 'success',
                duration: 500
              })
              this.$router.push('/innerRoleManageMain')
            } else {
              this.$message({
                message: res.data.message,
                type: 'error',
                duration: 1000
              })
            }
          })
        } else {
          // console.log('error submit!!')
          return false
        }
      })
    },
    judgePageType () {
      if (this.userId) {
        this.breadHeard = {
          title: '编辑员工',
          firstBread: '项目员工管理  ',
          secondBread: '编辑员工'
        }
        const params = {
          userId: +this.userId
        }
        this.$store.dispatch('projectRoleManage/readRoleDetail', params).then(res => {
          if (res.data.code === 200 && res.data.data) {
            this.sysUserDTO = { ...res.data.data }
          }
        })
      }
    }
  },
  mounted () {
    this.$store.dispatch('projectRoleManage/updateAllRoleList')
    this.$store.dispatch('projectRoleManage/updateDepartList')
    this.judgePageType()
  }
}
</script>

<style lang="scss" scoped>
.content{
  background-color: #fff;
  height: 700px;
  position: relative;
  ::v-deep .el-form{
    width: 800px;
    .el-form-item{
      margin-bottom: 30px;
    }
    .el-input{
      width: 400px;
      height: 48px;
      .el-input__inner{
        height: 100%;
      }
    }
    .el-textarea{
      width: 560px;
      height: 170px;
      .el-textarea__inner{
        height: 100%;
      }
    }
  .el-form-item__label{
    width: 300px !important;
    line-height: 48px !important;
    text-align: right;
    }
  .el-form-item__error{
    left: 300px;
    line-height: 18px;
  }
  .el-form-item.is-required:not(.is-no-asterisk)>.el-form-item__label:before{
    content: '*';
    color: #4868FF;
    margin-right: 4px;
    }
  }
 ::v-deep .el-upload--picture-card{
    width:390px;
    height:220px;
    background-color: #fff;
    .nopic{
      height: 100%;
      align-items: center;
      justify-content: center;
      position: relative;
      .addpics{
        line-height: 20px;
        height:20px;
        font-size:14px;
        font-family:PingFangSC-Regular,PingFang SC;
        font-weight:400;
        color:rgba(200,200,200,1);
      }
    }
      .pictips{
        line-height: 20px;
        position: absolute;
        top: 0;
        left: 586px;
        height:20px;
        font-size:14px;
        font-family:PingFangSC-Regular,PingFang SC;
        font-weight:400;
        color:rgba(255,180,21,1);
        cursor:default
      }
      .el-progress-circle{
        width:40px !important;
        height:40px !important;
      }
  }
}
.addBtn{
  padding-left: 300px;
  position: absolute;
  bottom: 30px;
  .confirm{
    width:172px;
    height:48px;
    background:rgba(72,104,255,1);
    border-radius:24px;
    font-size:18px;
    font-family:PingFangSC-Regular,PingFang SC;
    font-weight:400;
    color:rgba(255,255,255,1);
    cursor: pointer;
  }
}
</style>
